package UnSymmetric

import "../goEncrypt"
import "os"
import (
	"encoding/hex"
	"io/ioutil"
	"strings"
)

type ECC struct {
	publickeyfilepath  string
	privatekeyfilepath string
	publickey          string
	privatekey         string
}

func ReadFile(path string) string {
	file, err := os.Open(path)
	if err != nil {
		return ""
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	return string(data)
}

//读取加密，解密，删除，
//读取加密内存解密
func (e *ECC) SetPublickey(Publickey string) {
	e.publickey = Publickey
}
func (e *ECC) SetPrivatekey(Privatekey string) {
	e.privatekey = Privatekey
}
func (e *ECC) GetPublickey() string {
	return e.publickey
}
func (e *ECC) GetPrivatekey() string {
	return e.privatekey
}
func (e *ECC) Readpublickey(publickeypath string) {

	e.publickeyfilepath = publickeypath
	e.publickey = ReadFile(publickeypath)

}

func (e *ECC) Readprivatekey(privatekeypath string) {

	e.privatekeyfilepath = privatekeypath
	e.privatekey = ReadFile(privatekeypath)

}
func (e *ECC) MakeKey(publickeypath, privatekeypath string) {
	goEncrypt.EccPublishFileName = publickeypath
	goEncrypt.EccPrivateFileName = privatekeypath
	goEncrypt.GetEccKey() //生成ECC的公钥，私钥

	e.privatekeyfilepath = privatekeypath
	e.publickeyfilepath = publickeypath
	e.publickey = ReadFile(publickeypath)
	e.privatekey = ReadFile(privatekeypath)

}
func (e *ECC) GetPublicKey() string {

	return e.publickey
}
func (e *ECC) GetPrivateKey() string {
	return e.privatekey
}
func (e *ECC) Encrypt(datastr []byte) []byte {

	cryptText, err := goEncrypt.EccEncrypt(datastr, []byte(e.publickey))
	if err != nil {
		return []byte{}
	}
	return cryptText
}
func (e *ECC) Decrypt(datastr []byte) []byte {
	msg, err := goEncrypt.EccDecrypt(datastr, []byte(e.privatekey))
	if err != nil {
		return []byte{}
	}
	return msg
}

func (e *ECC) EncryptString(datastr string) string {
	cryptText, err := goEncrypt.EccEncrypt([]byte(datastr), []byte(e.publickey))
	if err != nil {
		return ""
	}
	return hex.EncodeToString(cryptText)
}
func (e *ECC) DecryptString(datastr string) string {
	debytes, _ := hex.DecodeString(datastr)
	msg, err := goEncrypt.EccDecrypt(debytes, []byte(e.privatekey))
	if err != nil {
		return ""
	}
	return string(msg)
}

func (e *ECC) EncryptFilePath(filepath string) string {

	file, err := os.Open(filepath)
	if err != nil {
		return ""
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	//crptdata:=d.Encrypt(data,[]byte(password)) //加密以后的数据
	crptdata := e.Encrypt(data)
	//fmt.Println(len(crptdata))
	savepath := makepath(filepath, true)
	savefile, err := os.Create(savepath)
	if err != nil {
		return ""
	}
	defer savefile.Close()
	savefile.Write(crptdata)
	//写入
	//返回路径

	return savepath
}
func (e *ECC) DecryptFilePath(filepath string) string {

	file, err := os.Open(filepath)
	if err != nil {
		return ""
	}
	defer file.Close()
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return ""
	}
	crptdata := e.Decrypt(data) //解密以后的数据
	//fmt.Println(len(crptdata))
	savepath := makepath(filepath, false)
	savefile, err := os.Create(savepath)
	if err != nil {
		return ""
	}
	defer savefile.Close()
	savefile.Write(crptdata)
	//写入
	//返回路径

	return savepath
}

func (e *ECC) SignString(datastr string) (string, string) {
	rtext, stext, err := goEncrypt.EccSign([]byte(datastr), []byte(e.privatekey))
	if err != nil {
		return "", ""
	}
	return string(rtext), string(stext)
}
func (e *ECC) VerificationSignString(datastr string, rtext string, stext string) bool {
	result := goEncrypt.EccVerifySign([]byte(datastr), []byte(e.publickey), []byte(rtext), []byte(stext))
	if result { // 如果result返回的是true那么就是本人签名，否则不是，只有私钥加密，相对的公钥验证才可以认为是本人
		return true
	} else {
		return false
	}

}

func (e *ECC) SignFilePath(filepath string) (string, string) {
	datastr := ReadFile(filepath)
	rtext, stext, err := goEncrypt.EccSign([]byte(datastr), []byte(e.privatekey))
	if err != nil {
		return "", ""
	}
	return string(rtext), string(stext)
}
func (e *ECC) VerificationSignPath(filepath string, rtext, stext string) bool {
	datastr := ReadFile(filepath)
	result := goEncrypt.EccVerifySign([]byte(datastr), []byte(e.publickey), []byte(rtext), []byte(stext))
	if result { // 如果result返回的是true那么就是本人签名，否则不是，只有私钥加密，相对的公钥验证才可以认为是本人
		return true
	} else {
		return false
	}
}
func makepath(path string, isen bool) string {
	filelist := strings.Split(path, "\\")
	if len(filelist) < 1 {
		return ""
	}
	lastpath := ""
	for i := 0; i < len(filelist)-1; i++ {
		lastpath += filelist[i] + "\\"
	}
	if isen {
		lastpath += "en_"
	} else {
		lastpath += "de_"
	}
	lastpath += filelist[len(filelist)-1]
	//fmt.Println(lastpath)
	return lastpath
}
